package com.zl.basis.screen;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Point;
import android.util.DisplayMetrics;
import android.util.TypedValue;
import android.view.Display;
import android.view.WindowManager;

import androidx.annotation.NonNull;

import java.util.Objects;

public class ScreenUtil {

    public static CommonSize getScreenSize(@NonNull Context context) {
        Resources res = context.getResources();
        DisplayMetrics metrics = res.getDisplayMetrics();
        return new CommonSize(metrics.widthPixels, metrics.heightPixels);
    }

    public static int getScreenWidth(@NonNull Context context) {
        Resources res = context.getResources();
        DisplayMetrics metrics = res.getDisplayMetrics();
        return metrics.widthPixels;
    }

    public static CommonSize getScreenSizeIncludeBar(@NonNull Context context) {
        WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        final Display display = Objects.requireNonNull(windowManager).getDefaultDisplay();
        Point outPoint = new Point();
        display.getRealSize(outPoint);
        return new CommonSize(outPoint.x, outPoint.y);
    }

    public static float dp2px(float dp) {
        return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, Resources.getSystem().getDisplayMetrics());
    }

    public static float sp2px(float sp) {
        return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, sp, Resources.getSystem().getDisplayMetrics());
    }
}
